package com.seedog.mq.distributed;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * 分布式配置类，用于配置分布式消息队列的相关参数
 */
public class DistributedConfig implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 默认配置参数
    private static final String DEFAULT_HOST = "localhost";
    private static final int DEFAULT_PORT = 7777;
    private static final int DEFAULT_HEARTBEAT_INTERVAL = 5000;
    private static final int DEFAULT_HEARTBEAT_TIMEOUT = 15000;
    private static final int DEFAULT_ELECTION_TIMEOUT = 30000;
    private static final int DEFAULT_REPLICATION_FACTOR = 3;
    private static final boolean DEFAULT_AUTO_ELECTION = true;
    private static final boolean DEFAULT_AUTO_RECOVERY = true;
    
    // 当前节点的主机名或IP地址
    private String host;
    // 当前节点的端口号
    private int port;
    // 心跳间隔（毫秒）
    private int heartbeatInterval;
    // 心跳超时时间（毫秒）
    private int heartbeatTimeout;
    // 选举超时时间（毫秒）
    private int electionTimeout;
    // 数据副本因子
    private int replicationFactor;
    // 是否自动进行主节点选举
    private boolean autoElection;
    // 是否自动恢复失败节点的数据
    private boolean autoRecovery;
    
    public DistributedConfig() {
        this.host = DEFAULT_HOST;
        this.port = DEFAULT_PORT;
        this.heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
        this.heartbeatTimeout = DEFAULT_HEARTBEAT_TIMEOUT;
        this.electionTimeout = DEFAULT_ELECTION_TIMEOUT;
        this.replicationFactor = DEFAULT_REPLICATION_FACTOR;
        this.autoElection = DEFAULT_AUTO_ELECTION;
        this.autoRecovery = DEFAULT_AUTO_RECOVERY;
    }
    
    // Getters and Setters
    public String getHost() {
        return host;
    }
    
    public void setHost(String host) {
        this.host = host;
    }
    
    public int getPort() {
        return port;
    }
    
    public void setPort(int port) {
        this.port = port;
    }
    
    public int getHeartbeatInterval() {
        return heartbeatInterval;
    }
    
    public void setHeartbeatInterval(int heartbeatInterval) {
        this.heartbeatInterval = heartbeatInterval;
    }
    
    public int getHeartbeatTimeout() {
        return heartbeatTimeout;
    }
    
    public void setHeartbeatTimeout(int heartbeatTimeout) {
        this.heartbeatTimeout = heartbeatTimeout;
    }
    
    public int getElectionTimeout() {
        return electionTimeout;
    }
    
    public void setElectionTimeout(int electionTimeout) {
        this.electionTimeout = electionTimeout;
    }
    
    public int getReplicationFactor() {
        return replicationFactor;
    }
    
    public void setReplicationFactor(int replicationFactor) {
        if (replicationFactor < 1) {
            throw new IllegalArgumentException("Replication factor must be at least 1");
        }
        this.replicationFactor = replicationFactor;
    }
    
    public boolean isAutoElection() {
        return autoElection;
    }
    
    public void setAutoElection(boolean autoElection) {
        this.autoElection = autoElection;
    }
    
    public boolean isAutoRecovery() {
        return autoRecovery;
    }
    
    public void setAutoRecovery(boolean autoRecovery) {
        this.autoRecovery = autoRecovery;
    }
    
    @Override
    public String toString() {
        return "DistributedConfig{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", heartbeatInterval=" + heartbeatInterval +
                ", heartbeatTimeout=" + heartbeatTimeout +
                ", electionTimeout=" + electionTimeout +
                ", replicationFactor=" + replicationFactor +
                ", autoElection=" + autoElection +
                ", autoRecovery=" + autoRecovery +
                '}';
    }
    
    /**
     * 创建默认配置
     * @return 默认配置实例
     */
    public static DistributedConfig getDefault() {
        return new DistributedConfig();
    }
    
    /**
     * 构建器模式
     */
    public static class Builder {
        private final DistributedConfig config;
        
        public Builder() {
            this.config = new DistributedConfig();
        }
        
        public Builder host(String host) {
            config.setHost(host);
            return this;
        }
        
        public Builder port(int port) {
            config.setPort(port);
            return this;
        }
        
        public Builder heartbeatInterval(int interval, TimeUnit timeUnit) {
            config.setHeartbeatInterval((int) timeUnit.toMillis(interval));
            return this;
        }
        
        public Builder heartbeatTimeout(int timeout, TimeUnit timeUnit) {
            config.setHeartbeatTimeout((int) timeUnit.toMillis(timeout));
            return this;
        }
        
        public Builder electionTimeout(int timeout, TimeUnit timeUnit) {
            config.setElectionTimeout((int) timeUnit.toMillis(timeout));
            return this;
        }
        
        public Builder replicationFactor(int factor) {
            config.setReplicationFactor(factor);
            return this;
        }
        
        public Builder autoElection(boolean autoElection) {
            config.setAutoElection(autoElection);
            return this;
        }
        
        public Builder autoRecovery(boolean autoRecovery) {
            config.setAutoRecovery(autoRecovery);
            return this;
        }
        
        public DistributedConfig build() {
            return config;
        }
    }
}